package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.CfgDictionaryInfo;
import com.baitengsoft.patentapimonitor.api.model.PacPatentInfo;
import com.baitengsoft.patentapimonitor.api.model.WebInvalidAnalysisInfo;
import com.baitengsoft.patentapimonitor.api.model.dto.WebInvalidAnalysisInfoDto;
import com.baitengsoft.patentapimonitor.api.model.dto.WebPacPatentInfoDto;
import com.baitengsoft.patentapimonitor.api.model.param.SearchExpiryNoticeParam;
import com.baitengsoft.patentapimonitor.api.service.CfgDictionaryService;
import com.baitengsoft.patentapimonitor.api.service.PacPatentinfoService;
import com.baitengsoft.patentapimonitor.api.service.WebExcelExportService;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.WebExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author Chen 2018/1/18
 */
@RestController
@RequestMapping("/api/v1/web/invalidAnalysis")
@Api(value = "前台-专利监控-失效分析",description = "失效分析")
public class WebInvalidAnalysisController extends AbstractController {
    @Autowired
    private CfgDictionaryService cfgDictionaryService;
    @Autowired
    private PacPatentinfoService pacPatentinfoService;
    @Autowired
    private WebExcelExportService webExcelExportService;
    /**
     * 获取筛选参数
     */
    @ApiOperation(value ="获取查询参数-专利类型(patTypes)")
    @GetMapping("/param")
    @ResponseBody
    public Res getPatType(){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        //获取专利类型和value
        List<CfgDictionaryInfo> patTypes = cfgDictionaryService.getNameAndValueByCode("1003");
        //获取失效原因
        Map<String, String> invoidLowStatus = cfgDictionaryService.getInvoidLowStatus();
        return Res.ok().put("patTypes",patTypes).put("invalidReason",invoidLowStatus);
    }

    @ApiOperation(value ="获取转入列表")
    @GetMapping("/list")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patType", value = "专利类型", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "invalidReason", value = "失效原因", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patNum", value = "申请号", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "ApplyPersonLast", value = "变更后权力人", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "startDate", value = "开始时间", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "endDate", value = "结束时间", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true)
    })
    public Res getInvalidAnalysisList(@ApiIgnore SearchExpiryNoticeParam param){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        PageResult<WebInvalidAnalysisInfo> result = pacPatentinfoService.getInvalidAnalysisList(userInfo.getPlatformid(), param);
        return Res.ok().put("list",result);
    }

    @ApiOperation(value ="获取转入列表")
    @GetMapping("/listexport")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patType", value = "专利类型", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "invalidReason", value = "失效原因", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patNum", value = "申请号", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "ApplyPersonLast", value = "变更后权力人", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "startDate", value = "开始时间", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "endDate", value = "结束时间", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true)
    })
    public Res getInvalidAnalysisList(@ApiIgnore SearchExpiryNoticeParam param, HttpServletResponse response){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        if (param.getPageSize()>5000)   return Res.error("最多可导出5000条数据");
        PageResult<WebInvalidAnalysisInfo> result = pacPatentinfoService.getInvalidAnalysisList(userInfo.getPlatformid(), param);
        if (result == null || result.getItems() == null || result.getItems().size() < 1) {
            return Res.error("无数据");
        }
        List<WebInvalidAnalysisInfo> infos = result.getItems();
        List<WebInvalidAnalysisInfoDto> webDtos = webExcelExportService.getWebInvalidAnalysisInfoDto(infos);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                "失效分析.xls",
                "失效分析");
    }
}
